import { ref } from 'vue';
import _ from 'lodash';
import { Message } from '@arco-design/web-vue';
import * as Stroe from '@/store';
import * as Api from '@/api/filmRead/filmRead';
import type * as StoreType from '@/store/modules/filmReadHeader/type';
export default function usePageRight() {
  const FilmReadHeader = Stroe.useFilmReadHeader();
  const FilmReadRight = Stroe.useFilmReadRight();

  const seriesSetFormRef = ref();
  const mjorSignsFormRef = ref();

  // 表单操作
  const handleChange = async (
    value:
      | string
      | number
      | boolean
      | (string | number | boolean)[]
      | Record<string, any>
      | (
          | string
          | number
          | Record<string, any>
          | (string | number | Record<string, any>)[]
        )[]
      | undefined,
    item: StoreType.FormOptionsItem,
    type: number
  ) => {
    if (item.parameter && item.parameter.isdefault) {
      // 为true调用变更征象接口
      const params = {
        arId: FilmReadHeader.seriesList && FilmReadHeader.seriesList[0].id,
        alrId: 0,
        acrList: [
          {
            componentId: item.componentId,
            result: '',
          },
        ],
      };
      if (Array.isArray(value)) {
        const newValue = _.cloneDeep(value);
        if (item.type === 'checkbox' || item.type === 'child_select') {
          params.acrList[0].result =
            newValue.length > 1 ? newValue.join('@#@') : newValue[0];
        }
      } else {
        value && (params.acrList[0].result = String(value));
      }
      if (type === 2 && FilmReadRight.currentSubmenuItem) {
        params.alrId = Number(FilmReadRight.currentSubmenuItem);
      }
      const res:any = await Api.saveCrListData(params);
      const { acrList } = res.data;
      item.acrId = acrList[0].acrId;
      item.id = acrList[0].acrId;
      // Message.success('新增成功！');
    } else {
      // isdefault为false调用新增
      if (item.acrId && value) {
        if (Array.isArray(value)) {
          const newValue = _.cloneDeep(value);
          const params = {
            acrId: item.acrId,
            result: newValue.length > 1 ? newValue.join('@#@') : newValue[0],
          };
          const res = await Api.updateCrListData(params);
          // Message.success('更新成功！');
          // getInitData();
        } else {
          const params = {
            acrId: item.acrId,
            result: String(value),
          };
          const res = await Api.updateCrListData(params);
          // Message.success('更新成功！');
        }
      } else {
        if (value) {
          const params = {
            arId: FilmReadHeader.seriesList && FilmReadHeader.seriesList[0].id,
            alrId: 0,
            acrList: [
              {
                componentId: item.componentId,
                result: '',
              },
            ],
          };
          if (Array.isArray(value)) {
            const newValue = _.cloneDeep(value);
            if (item.type === 'checkbox' || item.type === 'child_select') {
              params.acrList[0].result =
                newValue.length > 1 ? newValue.join('@#@') : newValue[0];
            }
          } else {
            value && (params.acrList[0].result = String(value));
          }
          if (type === 2 && FilmReadRight.currentSubmenuItem) {
            params.alrId = Number(FilmReadRight.currentSubmenuItem);
          }
          const res:any = await Api.saveCrListData(params);
          const { acrList } = res.data;
          item.acrId = acrList[0].acrId;
          item.id = acrList[0].acrId;
          // Message.success('新增成功！');
        }
      }
    }
  };
  return {
    seriesSetFormRef,
    mjorSignsFormRef,
    handleChange,
  };
}
